﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using DataAccess;
using com.chttl.ienc.bean;
using java.util;

namespace iEN.Report
{
    public partial class BillModeConfig : System.Web.UI.Page
    {
        #region protected void Page_Load(object sender, EventArgs e) //初始頁面
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)//先判斷是否已經登入過
            {                
                if (Session["LoginName"] == null)
                {
                    Response.Write("<script>alert('請先登入！！');location.href='../Login.aspx';</script>");
                }
                this.Intialize(); 
            }            
        }
        #endregion

        #region Initialization methods
        private void Intialize()
        {

            DataTable dtBillMode = new DBAccess().getBillModeTable();  //取得ALLTag
            Session["dtBillMode"] = dtBillMode;

            if (Session["SysAdmin"].ToString() != "2")
            {
                Panel1.Visible = false;
                btAddTag.Visible = false;
                lblMsg.Visible = true;
            }
        }
        #endregion

        #region //分頁設定
        protected void First_Click(object sender, EventArgs e)
        {
            int intPageIndex = 0;
            Button lkbtn = (Button)sender;
            switch (lkbtn.CommandArgument.ToString())
            {
                case "Previous":
                    if (this.gvBillMode.PageIndex > 0)
                        intPageIndex = this.gvBillMode.PageIndex - 1;
                    break;
                case "Next":
                    if (this.gvBillMode.PageIndex < this.gvBillMode.PageCount - 1)
                        intPageIndex = this.gvBillMode.PageIndex + 1;
                    break;
                case "Last":
                    intPageIndex = this.gvBillMode.PageCount - 1;
                    break;
            }
            //this.gvBillMode.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvBillMode.PageIndex = intPageIndex;
            this.gvBillMode.DataBind();
        }

        protected void Previous_Click(object sender, EventArgs e)
        {
            int intPageIndex = 0;
            Button lkbtn = (Button)sender;
            switch (lkbtn.CommandArgument.ToString())
            {
                case "Previous":
                    if (this.gvBillMode.PageIndex > 0)
                        intPageIndex = gvBillMode.PageIndex - 1;
                    break;
                case "Next":
                    if (this.gvBillMode.PageIndex < this.gvBillMode.PageCount - 1)
                        intPageIndex = this.gvBillMode.PageIndex + 1;
                    break;
                case "Last":
                    intPageIndex = gvBillMode.PageCount - 1;
                    break;
            }
            //this.gvBillMode.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvBillMode.PageIndex = intPageIndex;
            this.gvBillMode.DataBind();
        }

        protected void Next_Click(object sender, EventArgs e)
        {
            int intPageIndex = 0;
            Button lkbtn = (Button)sender;
            switch (lkbtn.CommandArgument.ToString())
            {
                case "Previous":
                    if (this.gvBillMode.PageIndex > 0)
                        intPageIndex = this.gvBillMode.PageIndex - 1;
                    break;
                case "Next":
                    if (this.gvBillMode.PageIndex < this.gvBillMode.PageCount - 1)
                        intPageIndex = gvBillMode.PageIndex + 1;
                    break;
                case "Last":
                    if (this.gvBillMode.PageIndex != this.gvBillMode.PageCount)
                        intPageIndex = this.gvBillMode.PageCount - 1;
                    break;
            }
            //this.gvBillMode.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvBillMode.PageIndex = intPageIndex;
            this.gvBillMode.DataBind();

        }

        protected void Last_Click(object sender, EventArgs e)
        {
            int intPageIndex = 0;
            Button lkbtn = (Button)sender;
            switch (lkbtn.CommandArgument.ToString())
            {
                case "Previous":
                    if (this.gvBillMode.PageIndex > 0)
                        intPageIndex = this.gvBillMode.PageIndex - 1;
                    break;
                case "Next":
                    if (this.gvBillMode.PageIndex < this.gvBillMode.PageCount - 1)
                        intPageIndex = this.gvBillMode.PageIndex + 1;
                    break;
                case "Last":
                    intPageIndex = this.gvBillMode.PageCount - 1;
                    break;
            }
            //this.gvBillMode.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvBillMode.PageIndex = intPageIndex;
            this.gvBillMode.DataBind();

        }

        protected void dd1PageJump_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlpage = ((DropDownList)this.gvBillMode.BottomPagerRow.FindControl("dd1PageJump"));
            //this.gvBillMode.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvBillMode.PageIndex = ddlpage.SelectedIndex;
            this.gvBillMode.DataBind();

        }

        public void pageSet(object gvQuery)
        {
            GridView gv = (GridView)gvQuery;
            if (gv.PageCount > 1)
            {
                if (gv.BottomPagerRow == null)
                    return;
                Label lbCount = (Label)gv.BottomPagerRow.FindControl("lbAllCount");
                Label lbPage = (Label)gv.BottomPagerRow.FindControl("lbAllPage");
                DropDownList ddlJump = (DropDownList)gv.BottomPagerRow.FindControl("dd1PageJump");
                //顯示資料總筆數與總頁數
                lbCount.Text = "共" + (gv.PageSize * gv.PageCount).ToString() + "筆";
                lbPage.Text = "共" + gv.PageCount + "頁";
                //設定下拉選單跳頁之頁數        
                for (int i = 1; i <= gv.PageCount; i++)
                    ddlJump.Items.Add(new ListItem(i.ToString()));
                //first:firstc和previous按鈕無作用;  last: next和last按鈕無作用  
                if (gv.PageIndex == 0)
                {
                    ((Button)gv.BottomPagerRow.FindControl("First")).Enabled = false;
                    ((Button)gv.BottomPagerRow.FindControl("Previous")).Enabled = false;
                }
                else if (gv.PageIndex == (gv.PageCount - 1))
                {
                    ((Button)gv.BottomPagerRow.FindControl("Next")).Enabled = false;
                    ((Button)gv.BottomPagerRow.FindControl("Last")).Enabled = false;
                }
                //顯示目前頁數
                ddlJump.SelectedIndex = gv.PageIndex;
            }
        }
        #endregion

        #region protected void btReturn_Click(object sender, EventArgs e) //回到設備管理頁面
        protected void btReturn_Click(object sender, EventArgs e)
        {
            //this.Page.Response.Redirect("../Organization/DeviceManage.aspx");
        }
        #endregion

        protected void gvBillMode_DataBound(object sender, EventArgs e)
        {
            pageSet(this.gvBillMode);
            gvBillMode.SelectedIndex = -1;
        }

        #region protected void btAddTag_Click(object sender, EventArgs e) //新增監控點
        protected void btAddTag_Click(object sender, EventArgs e)
        {
            
            gvBillMode.SelectedIndex = -1;
            Panel4.Visible = true; 
            Panel3.Visible = false;
            Panel5.Visible = false;
        }
        #endregion

        #region protected void gvBillMode_RowDeleting(object sender, GridViewDeleteEventArgs e) //刪除監控點
        protected void gvBillMode_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //DataTable dtTemp = (DataTable)Session["dtDataSourceTemp"];
            //int tagId = Convert.ToInt32(dtTemp.Rows[e.RowIndex]["_id"].ToString());
            //bool temp = new WebService().removeTag1(tagId);

            
        }
        #endregion


        #region protected void YesAdd_Click(object sender, EventArgs e) //確定新增
        protected void YesAdd_Click(object sender, EventArgs e)
        {
            
            try
            {
                DataTable dtBillModeSession = (DataTable)Session["dtBillMode"];

                DataTable dtBillModeCheck = dtBillModeSession.Copy();
                dtBillModeCheck.DefaultView.RowFilter = "_year=" + int.Parse(tbYearAdd.Text) + " and " +
                    "((_begin_time<='" + tbBeginTimeAdd.Text + "' and '" + tbBeginTimeAdd.Text + "'<=_end_time)" +
                    " or (_begin_time<='" + tbEndTimeAdd.Text + "' and '" + tbEndTimeAdd.Text + "'<=_end_time)" +
                    " or (_begin_time>='" + tbBeginTimeAdd.Text + "' and '" + tbEndTimeAdd.Text + "'>=_end_time))";
                dtBillModeCheck = dtBillModeCheck.DefaultView.ToTable();

                if (dtBillModeCheck != null && dtBillModeCheck.Rows.Count > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('" + tbYearAdd.Text + "年度已有此區段的電費資料，請確認時間沒有跟其他電費資料重疊！')", true);
                    return;
                }
                
                DataTable dtBillMode = dtBillModeSession.Copy();
                dtBillMode.DefaultView.RowFilter = "_year='" + tbYearAdd.Text + "' and _mode='" + ddlModeAdd.SelectedValue + "'";
                dtBillMode = dtBillMode.DefaultView.ToTable();
                if (dtBillMode != null && dtBillMode.Rows.Count > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('" + tbYearAdd.Text + "年度已有" + ddlModeAdd.SelectedItem + "資料！')", true);
                    return;
                }

                if ((ddlModeAdd.SelectedValue == "3" || ddlModeAdd.SelectedValue == "4") && tbBillAdd.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('寒暑假請輸入每度費用')", true);
                    return;
                }
                if(DateTime.Parse(tbBeginTimeAdd.Text) >= DateTime.Parse(tbEndTimeAdd.Text))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('結束時間不能小於等於開始時間')", true);
                    return;
                }

                    if (tbBillAdd.Text == "非寒暑假不需輸入")
                        tbBillAdd.Text = "0";


                    bool blnRet = new DBAccess().addBillModeData(tbYearAdd.Text,
                        DateTime.Parse(tbBeginTimeAdd.Text), DateTime.Parse(tbEndTimeAdd.Text),
                        ddlModeAdd.SelectedValue, double.Parse(tbBillAdd.Text));

                    string strAccount = Session["LoginAccount"].ToString();
                    string strIP = Session["LoginIP"].ToString();
                    string strMemo = "Year：" + tbYearAdd.Text + "//計費模式：" + ddlModeAdd.SelectedItem.Text;

                    if (blnRet == false)
                    {
                        new DBAccess().addEventLog(strAccount, strIP, 61, strMemo, "0");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增電費資料有誤！')", true);
                        //return;
                    }
                    {
                        new DBAccess().addEventLog(strAccount, strIP, 61, strMemo, "1");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增電費資料成功！')", true);
                        //return;
                    }

                    Panel4Clear();
                    Panel4.Visible = false;

                    Intialize();
                    gvBillMode.DataBind();

            }
            catch
            {

                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "Year：" + tbYearAdd.Text + "//計費模式：" + ddlModeAdd.SelectedValue;
                new DBAccess().addEventLog(strAccount, strIP, 61, strMemo, "0");

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增電費資料有誤，請檢查輸入資料格式！')", true);
            }
        }
        #endregion



        protected void gvBillMode_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                switch (e.Row.Cells[2].Text)
                {
                    case "1":
                        e.Row.Cells[2].Text = "上學期";
                        break;
                    case "2":
                        e.Row.Cells[2].Text = "下學期";
                        break;
                    case "3":
                        e.Row.Cells[2].Text = "暑假";
                        break;
                    case "4":
                        e.Row.Cells[2].Text = "寒假";
                        break;
                }
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {

            }
        }

        #region private void Panel4Clear() //清除新增資料
        private void Panel4Clear()
        {
            tbYearAdd.Text = "";
            tbBeginTimeAdd.Text = "";
            tbEndTimeAdd.Text = "";
            ddlModeAdd.SelectedIndex = 1;
            tbBillAdd.Text = "";
        }
        #endregion

        #region private void Panel3Clear() //清除修改資料
        private void Panel3Clear()
        {
            tbYearUpdate.Text = "";
            tbBeginTimeUpdate.Text = "";
            tbEndTimeUpdate.Text = "";
            ddlModeUpdate.SelectedIndex = 1;
            tbBillUpdate.Text = "";
        }
        #endregion

        protected void ddlModeAdd_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlModeAdd.SelectedValue == "3" || ddlModeAdd.SelectedValue == "4")
            {
                tbBillAdd.Enabled = true;
                tbBillAdd.Text = "";
            }
            else
            {
                tbBillAdd.Enabled = false;
                tbBillAdd.Text = "非寒暑假不需輸入";
            }
        }

        #region protected void gvBillMode_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) //修改監控點
        protected void gvBillMode_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            //this.UpdatePanel3.Visible = false;            
            //this.UpdatePanel2.Visible = true;

            Panel3.Visible = true;
            Panel4.Visible = false;
            

            Session["EditIndex"] = e.NewSelectedIndex;
            Session["BillID"] = this.gvBillMode.Rows[e.NewSelectedIndex].Cells[0].Text;
            string strMode = this.gvBillMode.Rows[e.NewSelectedIndex].Cells[2].Text;

            tbYearUpdate.Text = this.gvBillMode.Rows[e.NewSelectedIndex].Cells[1].Text;
            tbBeginTimeUpdate.Text = this.gvBillMode.Rows[e.NewSelectedIndex].Cells[3].Text;
            tbEndTimeUpdate.Text = this.gvBillMode.Rows[e.NewSelectedIndex].Cells[4].Text;
            ddlModeUpdate.SelectedIndex = -1;
            for (int i = 0; i < ddlModeUpdate.Items.Count; i++)
            {
                if (ddlModeUpdate.Items[i].Text == this.gvBillMode.Rows[e.NewSelectedIndex].Cells[2].Text)
                    ddlModeUpdate.Items[i].Selected = true;
            }

            if (strMode == "暑假" || strMode == "寒假")
            {
                DataTable dtTag = new DBAccess().getBillModeBillTable(this.gvBillMode.Rows[e.NewSelectedIndex].Cells[0].Text);
                tbBillUpdate.Text = dtTag.Rows[0]["_bill"].ToString();
                tbBillUpdate.Enabled = true;

                gvBillModeBill.DataBind();
                Panel5.Visible = false;
            }
            else
            {
                tbBillUpdate.Text = "非寒暑假不需輸入";
                tbBillUpdate.Enabled = false;
                Panel5.Visible = true;
                
            }

            
        }
        #endregion

        #region protected void btnComfirm_Click(object sender, EventArgs e) //確定修改
        protected void btnComfirm_Click(object sender, EventArgs e)
        {
            string strID = Session["BillID"].ToString();


            try
            {
                DataTable dtBillModeSession = (DataTable)Session["dtBillMode"];

                DataTable dtBillModeCheck = dtBillModeSession.Copy();
                dtBillModeCheck.DefaultView.RowFilter = "_year=" + int.Parse(tbYearUpdate.Text) + " and _id <>'" + strID + "' and" +
                    "((_begin_time<='" + tbBeginTimeUpdate.Text + "' and '" + tbBeginTimeUpdate.Text + "'<=_end_time)" +
                    " or (_begin_time<='" + tbEndTimeUpdate.Text + "' and '" + tbEndTimeUpdate.Text + "'<=_end_time)" +
                    " or (_begin_time>='" + tbBeginTimeUpdate.Text + "' and '" + tbEndTimeUpdate.Text + "'>=_end_time))";
                dtBillModeCheck = dtBillModeCheck.DefaultView.ToTable();

                if (dtBillModeCheck != null && dtBillModeCheck.Rows.Count > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('" + tbYearUpdate.Text + "年度已有此區段的電費資料，請確認時間沒有跟其他電費資料重疊！')", true);
                    return;
                }
                
                
                if (DateTime.Parse(tbBeginTimeUpdate.Text) >= DateTime.Parse(tbEndTimeUpdate.Text))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('結束時間不能小於等於開始時間')", true);
                    return;
                }
                
                bool blnRet = new DBAccess().updateBillModeData(strID, DateTime.Parse(tbBeginTimeUpdate.Text),
                    DateTime.Parse(tbEndTimeUpdate.Text), double.Parse(tbBillUpdate.Text));

                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "ID：" + strID + "Year：" + tbYearUpdate.Text + "//計費模式：" + ddlModeUpdate.SelectedItem.Text;

                if (blnRet == false)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 63, strMemo, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改電費資料有誤！')", true);
                    //return;
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 63, strMemo, "1");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改電費資料成功！')", true);
                    //return;
                }
            }
            catch
            {
                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "Year：" + tbYearUpdate.Text + "//計費模式：" + ddlModeUpdate.SelectedValue;
                new DBAccess().addEventLog(strAccount, strIP, 63, strMemo, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改電費資料有誤！')", true);
            }
            Panel3Clear();
            Panel3.Visible = false;
            //this.UpdatePanel2.Visible = false;
            //this.Page.Response.Redirect("../Organization/TagManage.aspx");
            Intialize();
            gvBillMode.DataBind();
        }
        #endregion

        #region protected void btnCancelUpdate_Click(object sender, EventArgs e) //取消修改
        protected void btnCancelUpdate_Click(object sender, EventArgs e)
        {
            Panel4.Visible = false;
        }
        #endregion

        protected void ddlModeUpdate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlModeUpdate.SelectedValue == "3" || ddlModeUpdate.SelectedValue == "4")
            {
                tbBillUpdate.Enabled = true;
                tbBillUpdate.Text = "";
            }
            else
            {
                tbBillUpdate.Enabled = false;
                tbBillUpdate.Text = "非寒暑假不需輸入";
            }
        }

        #region protected void NoEdit_Click(object sender, EventArgs e) //取消新增
        protected void NoEdit_Click(object sender, EventArgs e)
        {
            Panel3.Visible = false;
        }
        #endregion

        protected void gvBillMode_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
            if (e.CommandName == "Del")
            {
                int iIndex = Convert.ToInt32(e.CommandArgument);   //取得第幾列被選取 

                string strID = this.gvBillMode.Rows[iIndex].Cells[0].Text;

                bool blnRet = new DBAccess().deleteBillModeData(strID);

                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "ID：" + strID + "Year：" + this.gvBillMode.Rows[iIndex].Cells[1].Text + "//計費模式：" + this.gvBillMode.Rows[iIndex].Cells[2].Text;

                if (blnRet == false)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 62, strMemo, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('刪除電費資料有誤！')", true);
                    //return;
                }
                {
                    new DBAccess().addEventLog(strAccount, strIP, 62, strMemo, "1");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('刪除電費資料成功！')", true);
                    //return;
                }
                Intialize();
                gvBillMode.DataBind();

            }
        }

        protected void btnBillAdd_Click(object sender, EventArgs e)
        {
            
            try
            {
                string strID = Session["BillID"].ToString();
                int intBillEnd = 9999999;
                if (tbBillEnd.Text != "")
                    intBillEnd = int.Parse(tbBillEnd.Text);

                int intBillStart = int.Parse(tbBillStart.Text);

                DataTable dtBillModeBill = new DBAccess().getBillModeBillTable(strID);
                dtBillModeBill.DefaultView.RowFilter = "(_kwh_start<='" + intBillStart + "' and '" + intBillStart + "'<=_kwh_end)" +
                    " or (_kwh_start<='" + intBillEnd + "' and '" + intBillEnd + "'<=_kwh_end)" +
                    " or (_kwh_start>='" + intBillStart + "' and '" + intBillEnd + "'>=_kwh_end)";
                dtBillModeBill = dtBillModeBill.DefaultView.ToTable();

                if (dtBillModeBill != null && dtBillModeBill.Rows.Count > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('已有此區段的用電度數資料，請確認用電度數沒有跟其他資料重疊！')", true);
                    return;
                }

                bool blnRet = new DBAccess().addBillModeBillData(strID, intBillStart, intBillEnd, double.Parse(tbBill.Text));

                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "ID：" + strID + "//Start：" + intBillStart.ToString() + "//End：" + intBillEnd.ToString() + "//Bill：" + tbBill.Text;

                if (blnRet == false)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 64, strMemo, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增電費資料有誤！')", true);
                    //return;
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 64, strMemo, "1");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增電費資料成功！')", true);
                    //return;
                }
            }
            catch
            {
                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "ID：" + Session["BillID"].ToString() + "//Start：" + tbBillStart.Text + "//End：" + tbBillEnd.Text + "//Bill：" + tbBill.Text;
                new DBAccess().addEventLog(strAccount, strIP, 64, strMemo, "0");

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增電費資料有誤！')", true);
            }

            gvBillModeBill.DataBind();
        }

        protected void gvBillModeBill_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[2].Text == "9999999")
                    e.Row.Cells[2].Text = "以上";


                e.Row.Cells[0].Visible = false;
                e.Row.Cells[4].Visible = false;
            }
            else if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false;
                e.Row.Cells[4].Visible = false;
            }
        }

        protected void gvBillModeBill_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int iIndex = Convert.ToInt32(e.CommandArgument);   //取得第幾列被選取 

                try
                {
                    string strID = this.gvBillModeBill.Rows[iIndex].Cells[0].Text;
                    int intkwh_start = int.Parse(this.gvBillModeBill.Rows[iIndex].Cells[1].Text);
                    int intkwh_end = 9999999;
                    if (gvBillModeBill.Rows[iIndex].Cells[2].Text != "以上")
                        intkwh_end = int.Parse(this.gvBillModeBill.Rows[iIndex].Cells[2].Text);

                    bool blnRet = new DBAccess().deleteBillModeBillData(strID, intkwh_start, intkwh_end);

                    string strAccount = Session["LoginAccount"].ToString();
                    string strIP = Session["LoginIP"].ToString();
                    string strMemo = "ID：" + strID + "//Start：" + intkwh_start.ToString() + "//End：" + intkwh_end.ToString();

                    if (blnRet == false)
                    {
                        new DBAccess().addEventLog(strAccount, strIP, 65, strMemo, "0");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('刪除電費資料有誤！')", true);
                        //return;
                    }
                    {
                        new DBAccess().addEventLog(strAccount, strIP, 65, strMemo, "1");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('刪除電費資料成功！')", true);
                        //return;
                    }

                    gvBillModeBill.DataBind();
                }
                catch
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('刪除電費資料成功！')", true);
                }
            }
        }


    }
}